<?php
/**
 * Application Controller
 * The application controller actually should extend this in case it needs to
 * add any of its own functionality, but it doesn't have to. You could just use
 * this is your app controller.
 *
 * @package     Visi
 * @subpackage  Controllers
 * @author      Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright   (c) 2011 Coalition Technologies <joel@coalitiontechnologies.com>
 * @version     $Id$
 */
class Visi_Controller_Action extends Zend_Controller_Action {

    /**
     * Set the flash messenger object without a redirect. This delegates to the
     * FlashMessenger object.
     *
     * @param string The flash message
     * @param string The flash namespace (success, notice, error)
     * @return void
     */
    protected function _flash($message, $namespace = null) {
    
        $this->_helper->flashMessenger->flash($message, $namespace);
    
    }
    
    /**
     * Send user to a particular route and include a flash message. This
     * delegates to both FlashMessenger and Zend's Redirector action helpers.
     *
     * @param string The flash message
     * @param string The flash namespace (success, notice, error)
     * @param string The name of the route
     * @param array  An associative array of route params
     * @param bool   Set to true to prevent params from defaulting to the
     *               current route's params
     * @return void
     */
    protected function _flashToRoute($message, $namespace, $routeName, $urlOptions = array(), $reset = false) {
    
        $this->_helper->flashMessenger($message, $namespace);
        $this->_goToRoute($routeName, $urlOptions, $reset);
    
    }
    
    /**
     * Send user to a particular route. This delegates to the Zend Redirector
     * action helper.
     *
     * @param string The name of the route
     * @param array  An asssociative array of route params
     * @param bool   Set to true to prevent params from defaulting to the
     *               current route's params
     * @return void
     */
    protected function _goToRoute($name, $urlOptions = array(), $reset = false) {
    
        $this->_helper->redirector->goToRoute($urlOptions, $name, $reset);
    
    }
    
    /**
     * Retreive an instance of a model object. This delegates to the
     * ModelCatalog action helper.
     *
     * @param string The name of the model you want an instance of. For single-
     *               word objects, just use the name (DbTable_User = "user"). 
     *               For multi-word objects, use lower-case with underscoress
     *               as spaces (DbTable_UserVote = "user_vote").
     * @return Visi_Db_Table object
     */
    protected function _getModel($model) {
    
        return $this->_helper->modelCatalog($model);
    
    }
    
    /**
     * Retreive a record object or throw a 404 not found exception (generally 
     * resulting in a 404 "not found" page being displayed).
     * 
     * @todo I have implemented this in most places, but not all
     * @todo Maybe this should be renamed _fetchRecordOr404()
     * @todo Maybe the logic for this should be extracted into an action helper
     *       with this just as a shortcut to it.
     */
    protected function _fetchObjectOr404($model, $pk, $message = 'The object you are trying to find does not exist.') {
    
        $result = $this->_getModel($model)->find($pk);
        if (!$result->count()) {
            throw new Visi_Controller_Response_Exception_NotFound($message);
        }
        return $result->current();
    
    }

    /**
     * Generates an error report for logging
     */
    protected function _generateErrorReport($exception, $template = 'elements/error-report.phtml') {
    
        return $this->view->partial($template, array('exception' => $exception));
    
    }
    
    protected function _email($subject, $body, $to, $from = null) {
    
        $mail = new Zend_Mail;
        if (is_array($to)) {
            $mail->addTo(current($to), key($to));
        } else {
            $mail->addTo($to);
        }
        // from can be set by default in the bootstrap
        if (!is_null($from)) {
            if (is_array($from)) {
                $mail->setFrom(current($from), key($from));
            } else {
                $mail->setFrom($from);
            }
        }
        $mail->setSubject($subject);
        $mail->setBodyText($body);
        return $mail->send();
    
    }

}